package src.main.yuanxing;

public class User implements Cloneable{
    private String username = "张三";
    private String password = "123456";
    private  PersonO personO=new PersonO();
    private  PersonO2 personO2=new PersonO2();

    public PersonO2 getPersonO2() {
        return personO2;
    }

    public void setPersonO2(PersonO2 personO2) {
        this.personO2 = personO2;
    }


    public PersonO getPersonO() {
        return personO;
    }

    public void setPersonO(PersonO personO) {
        this.personO = personO;
    }

    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }

    @Override
    public User clone() throws CloneNotSupportedException {
        // TODO Auto-generated method stub
        User user=(User)super.clone();
        user.setPersonO2(this.personO2.clone());
        return user;
    }


    public static void main(String[] args) {
        try {
            User user01 = new User();
            User user02 = user01;
            System.out.println("1:浅拷贝----");
            System.out.println(user01+"--"+user01.getUsername()+user01.getPassword()+"||"+user01.getPersonO()+"||"+user01.getPersonO().getName());
            System.out.println(user02+"--"+user02.getUsername()+user02.getPassword()+"||"+user02.getPersonO()+"||"+user02.getPersonO().getName());
            user02.setUsername("王五");
            user02.setPassword("666");
            user02.getPersonO().setName("王五");
            System.out.println("修改 user02 属性--》01也被改动");
            System.out.println(user01+"--"+user01.getUsername()+user01.getPassword()+"||"+user01.getPersonO()+"||"+user01.getPersonO().getName());
            System.out.println(user02+"--"+user02.getUsername()+user02.getPassword()+"||"+user02.getPersonO()+"||"+user02.getPersonO().getName());
            System.out.println("-----------------------------------------------");

            User user011 = new User();
            User user022 = user011.clone();
            System.out.println("1:深拷贝 user011.clone()----");
            System.out.println(user01+"--"+user011.getUsername()+user011.getPassword()+"||"+user011.getPersonO()+"||"+user011.getPersonO().getName());
            System.out.println(user02+"--"+user022.getUsername()+user022.getPassword()+"||"+user022.getPersonO()+"||"+user022.getPersonO().getName());
            user022.setUsername("王五2");
            user022.setPassword("66666");
            user022.getPersonO().setName("王五2");
            System.out.println("修改 user022 属性--》011不会也被改动，但011 中引用对象还是浅拷贝");
            System.out.println(user011+"--"+user011.getUsername()+user011.getPassword()+"||"+user011.getPersonO()+"||"+user011.getPersonO().getName());
            System.out.println(user022+"--"+user022.getUsername()+user022.getPassword()+"||"+user022.getPersonO()+"||"+user022.getPersonO().getName());
            System.out.println("-----------------------------------------------");

            User user0111 = new User();
            User user0222 = user0111.clone();
            System.out.println("1:深拷贝 user0111.clone()----");
            System.out.println(user01+"--"+user0111.getUsername()+user0111.getPassword()+"||"+user0111.getPersonO2()+"||"+user0111.getPersonO2().getName());
            System.out.println(user02+"--"+user0222.getUsername()+user0222.getPassword()+"||"+user0222.getPersonO2()+"||"+user0222.getPersonO2().getName());
            user0222.setUsername("王五3");
            user0222.setPassword("66667");
            PersonO2 personO2=new PersonO2();
            personO2.setName("小明"); //修改原对象
            user0111.setPersonO2(personO2);
            user0222.getPersonO2().setName("王五3");
            System.out.println("修改 user0222 属性--》0111不会也被改动，0111 中引用PersonO2对象也实现clone(),并且User clone()方法中setPersonO2(this.personO2.clone()) ");
            System.out.println(user0111+"--"+user0111.getUsername()+user0111.getPassword()+"||"+user0111.getPersonO2()+"||"+user0111.getPersonO2().getName());
            System.out.println(user0222+"--"+user0222.getUsername()+user0222.getPassword()+"||"+user0222.getPersonO2()+"||"+user0222.getPersonO2().getName());
            System.out.println("-----------------------------------------------");


        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}


class PersonO implements Cloneable{
    public String name="李四";

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

}

class PersonO2 implements Cloneable{
    public String name="李四";

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public PersonO2 clone() {
        try {
            PersonO2 clone = (PersonO2) super.clone();
            // TODO: 复制此处的可变状态，这样此克隆就不能更改初始克隆的内部
            return clone;
        } catch (CloneNotSupportedException e) {
            throw new AssertionError();
        }
    }
}